package org.techmore.biz;

import java.util.ArrayList;
import java.util.List;

import org.techmore.utils.DatabaseHelper;
import org.techmore.utils.PropertiesUtil;
import org.techmore.vo.CouponDetailsVO;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class MyCouponListActivityDataHandler {
	
	private static MyCouponListActivityDataHandler myCouponListActivityDataHandler = new MyCouponListActivityDataHandler();
	
	public static MyCouponListActivityDataHandler getInstance(){
		return myCouponListActivityDataHandler;
	}
	
	public List<CouponDetailsVO> getMyCouponList(Context context){
		List<CouponDetailsVO> list = new ArrayList<CouponDetailsVO>();
		String databaseName = PropertiesUtil.getInstance().getValue("databaseName");
		SQLiteDatabase sqLiteDatabase = null;
		Cursor cursor = null;
		CouponDetailsVO couponDetailsVO = null;
		String sql ="select * from couponDetailsVO";
		try {
			list.clear();
			sqLiteDatabase = new DatabaseHelper(context, databaseName).getReadableDatabase();
			cursor = sqLiteDatabase.rawQuery(sql, new String[]{});
			while(cursor.moveToNext()){
				//couponSeriesNo varchar(24),store varchar(100),subject varchar(100),
				//couponImgUrl varchar(200),startDate varchar(20),
				//endDate varchar(20),introduction text,couponNo varchar(30),qrImageUrl varchar(200))
				couponDetailsVO = new CouponDetailsVO();
				couponDetailsVO.setCouponSeriesNo(cursor.getString(cursor.getColumnIndex("couponSeriesNo")));
				couponDetailsVO.setStore(cursor.getString(cursor.getColumnIndex("store")));
				couponDetailsVO.setSubject(cursor.getString(cursor.getColumnIndex("subject")));
				couponDetailsVO.setImg48xUrl(cursor.getString(cursor.getColumnIndex("img48xUrl")));
				couponDetailsVO.setCouponImgUrl(cursor.getString(cursor.getColumnIndex("couponImgUrl")));
				couponDetailsVO.setStartDate(cursor.getString(cursor.getColumnIndex("startDate")));
				couponDetailsVO.setEndDate(cursor.getString(cursor.getColumnIndex("endDate")));
				couponDetailsVO.setIntroduction(cursor.getString(cursor.getColumnIndex("introduction")));
				couponDetailsVO.setCouponNo(cursor.getString(cursor.getColumnIndex("couponNo")));
				couponDetailsVO.setQrImageUrl(cursor.getString(cursor.getColumnIndex("qrImageUrl")));
				list.add(couponDetailsVO);
			}
		} catch (Exception e) {
			Log.e("sqlite:", e.getMessage());
		}finally{
			cursor.close();
			sqLiteDatabase.close();
		}
//		System.out.println(list.size());
		return list;
	}

}
